package jp.sourceforge.akjrcp.text_douga.item;

import java.awt.Rectangle;
import java.awt.Shape;

import jp.sourceforge.akjrcp.commons.util.JapaneseUtils;

import org.apache.commons.lang.ArrayUtils;

/*
 * normal image
 */
public class KaraokeItem extends TextItem{
private double[] timing;
public static char[] whitespaces={' ','\t','\n',JapaneseUtils.JAPANESE_SPACE};
private int selectionStrings=0;
private double progressed=0;
public KaraokeItem(String text) {
	super(text);
}

public String toString(){
	return "KaraokeItem:"+super.toString()+" timing="+ArrayUtils.toString(timing);
}

public double[] getTiming() {
	return timing;
}

public void setTiming(double[] timing) {
	this.timing = timing;
}

public void beforeWriteFrame(double time){
	if(timing==null){
		initDefaultTiming();
	}
	if(time<timing[0]){
		return;//do nothing 
	}
	double total=0;
	progressed=0;
	for(int i=0;i<timing.length;i++){
		total+=timing[i];
		if(time>=total){
			selectionStrings=(i);
			//log.warn("selectionStrings:"+selectionStrings+" time="+time);
		}else{
			//set progress
			progressed=(time-(total-timing[i]))/timing[i];
			log.info("progressed:"+progressed+" time="+time);
			break;
		}
	}
}

//for subclass
public Shape getSelectionShape(int index){
	
	//
	if(selectionStrings==0 && index==0){//only first character
		if(progressed>0 &&progressed<1){
			Shape selected2=textLinesLayout.getTextLayouts()[0].getLogicalHighlightShape(0, 1);
			Rectangle rect2=selected2.getBounds();
			rect2.width=(int) (rect2.width*progressed);
			return rect2;
		}
		return null;
	}
	
	int total=0;
	String lines[]=textLinesLayout.getTexts();
	for (int i = 0; i <=index; i++) {
		int characters=countNotWhiteSpace(lines[i]);
		log.trace("selectionStrings:"+selectionStrings+" v="+characters);
		if(selectionStrings>total&&selectionStrings<=characters+total){
			if(i==index){
				int selected=selectNotWhiteSpace(lines[i],selectionStrings-total);
				log.trace("selectionStrings:"+selectionStrings+" v="+characters+" select="+selected);
				
				//return 
				Rectangle rectangle=new Rectangle();
				Shape selected1=textLinesLayout.getTextLayouts()[i].getLogicalHighlightShape(0, selected);
				rectangle.add(selected1.getBounds());
				log.trace("added1:"+selected1.getBounds());
				if(progressed>0&&progressed<1){
					try{
					Shape selected2=textLinesLayout.getTextLayouts()[i].getLogicalHighlightShape(0, selected+1);
					int dw=(int) ((selected2.getBounds().width-selected1.getBounds().width)*progressed);
					Rectangle rect2=selected1.getBounds();
					//log.trace("additional:"+dw);
					rect2.width+=dw;
					rectangle.add(rect2);
					log.trace("added2:"+rect2);
					}catch(Exception e){
						log.warn(e.getMessage());
						}
					}
				return rectangle;
			}
		}else if(selectionStrings>characters+total && i==index){
			//fix 6.0.3 bug
			Shape selected1=textLinesLayout.getTextLayouts()[i].getLogicalHighlightShape(0, lines[i].length());//select all
			return selected1.getBounds();
		}
		total+=characters;
	}
	
	return null;
}

private int selectNotWhiteSpace(String text,int selection){
	if(selection==0){
		return 0;
	}
	int c=0;
	for(int i=0;i<text.length();i++){
		boolean w=false;
		for (int j = 0; j < whitespaces.length; j++) {
			if(text.charAt(i)==whitespaces[j]){
				w=true;
			}
		}
		
		if(!w){
			c++;
		}
		if(c==selection){
			return i+1;
		}
	}
	return text.length();
}

private int countNotWhiteSpace(String text){
	int c=0;
	for(int i=0;i<text.length();i++){
		boolean w=false;
		for (int j = 0; j < whitespaces.length; j++) {
			if(text.charAt(i)==whitespaces[j]){
				w=true;
			}
		}
		if(!w){
			c++;
		}
	}
	return c;
}

private void initDefaultTiming(){
	int total=0;
	String lines[]=textLinesLayout.getTexts();
	for (int i = 0; i < lines.length; i++) {
		total+=countNotWhiteSpace(lines[i]);
	}
	double v=(duration-0.1)/total;	//it work for 10fps
	timing=new double[total+1];
	timing[0]=0;//start time
	for (int i = 1; i < total+1; i++) {
		timing[i]=v;
	}
}

}
